package com.example.laboratory.root_ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.laboratory.R;
import com.example.laboratory.bean.TreeNode;

import java.util.ArrayList;

public class TreeAdapter extends BaseAdapter{
    /** 所有的数据集合 */
    private ArrayList<TreeNode> allNodes;
    /** 顶层元素结合*/
    private ArrayList<TreeNode> topNodes;
    /** LayoutInflater */
    private LayoutInflater inflater;
    /** item的行首缩进基数 */
    private int indentionBase;

    private Context mContext;
    public TreeAdapter(Context context,ArrayList<TreeNode> topNodes, ArrayList<TreeNode> allNode) {
        this.topNodes = topNodes;
        this.allNodes = allNode;
        this.mContext = context;
        indentionBase =20;
    }

    public ArrayList<TreeNode> getTopNodes() {
        return topNodes;
    }

    public ArrayList<TreeNode> getAllNodes() {
        return allNodes;
    }
    @Override
    public int getCount() {
        return topNodes.size();
    }

    @Override
    public Object getItem(int position) {
        return topNodes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_tree, null);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.treeText = (TextView) convertView.findViewById(R.id.treeText);

        TreeNode element = topNodes.get(position);
        int level =element.getLevel();


        holder.treeText.setPadding(
                indentionBase * (level + 1),
                holder.treeText.getPaddingTop(),
                holder.treeText.getPaddingRight(),
                holder.treeText.getPaddingBottom());
        holder.treeText.setText(element.getContentText());
        if (element.isHasChildren() && !element.isExpanded()) {

        } else if (element.isHasChildren() && element.isExpanded()) {

        } else if (!element.isHasChildren()) {
        }
        //convertView.setOn

        return convertView;
    }

    static class ViewHolder{
        TextView treeText;
    }
}
